package com.accessibleworlds.blind;

import java.util.ArrayList;

import com.accessibleworlds.R;
import com.accessibleworlds.util.SharedVariables;
import com.accessibleworlds.util.TextToSpeeches;
import com.accessibleworlds.util.TextToSpeechesGoogle;
import com.accessibleworlds.util.TextToSpeechesPico;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

/**
 * Represents the select violation type for blind users.
 * @author Ayberk Özgür
 *
 */
public class BlindUserSelectViolationType extends Activity implements MultitouchGestureListener {	
	private TextToSpeeches tts = null;
	private boolean ttsReady = false;
	private Thread connectionThread;
	private ArrayList<BlindUserMenuItem> spokenMenu = null;
	private int currentItem = 0;
	private int numberOfItems = 8;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.blinduserpage);

		//Populate the menu
		spokenMenu = new ArrayList<BlindUserMenuItem>();
		for(int i=0;i<8;i++)
			spokenMenu.add(new BlindUserMenuItem(this, getResources().getStringArray(R.array.blind_violation_names)[i], null, false));
		
		connectionThread = new Thread(new Runnable() {
			public void run() {
				connection();
			}
		});
		connectionThread.start();
	}

	@Override
	public void onBackPressed() {
	}

	private void connection() {
		if("0".equalsIgnoreCase(getResources().getStringArray(R.array.languageValues)[SharedVariables.currentLanguage]))
			tts = new TextToSpeechesPico();
		else
			tts = new TextToSpeechesGoogle();
		tts.speak(BlindUserSelectViolationType.this, getResources().getString(R.string.blind_select_violation_type) + "Current item: " + spokenMenu.get(currentItem).getSpokenName());
		ttsReady = true;
	}

	public void multitouchMotionEvent(int numberOfFingers, GestureType gestureType) {
		if(ttsReady){
			tts.stopSpeaking();
			switch(gestureType){
			case DOWN:
				if(numberOfItems > 0){
					currentItem = (currentItem + 1) % numberOfItems;
					tts.speak(BlindUserSelectViolationType.this, spokenMenu.get(currentItem).getSpokenName());
				}
				break;
			case UP:
				if(numberOfItems > 0){
					currentItem--;
					if(currentItem < 0)
						currentItem = numberOfItems - 1;
					tts.speak(BlindUserSelectViolationType.this, spokenMenu.get(currentItem).getSpokenName());
				}
				break;
			case LEFT:
			case RIGHT:
				Intent resultIntent = new Intent();
				resultIntent.putExtra("result", currentItem);
				resultIntent.putExtra("item_to_get", "violation_type");
				BlindUserSelectViolationType.this.setResult(RESULT_OK, resultIntent);
				finish();
				break;
			}
		}
	}
}
